package com.bamboo.core.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
* @description 时间、日期相关的常用转换工具
* @date 2025/3/6 17:34
* @author chenzh
*/
public class TimeUtil {
    static final String COMMON_DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    static final String DATETIME_FORMATTER_MILL = "yyyy-MM-dd HH:mm:ss:SSS";

    /**
     * @description 获取当前时间戳
     * @date 2025/3/7 11:14
     * @author chenzh
     * @return long
     */
    public static long nowSecond() {
        return System.currentTimeMillis() / 1000;
    }
    /**
     * @description 获取当前日期的时间戳
     * @date 2025/3/7 8:38
     * @author chenzh
     * @return String
     */
    public static String nowDatetime() {

        LocalDateTime now =LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeUtil.COMMON_DATETIME_FORMATTER);
        return now.format(dateTimeFormatter);
    }

    public static String nowDatetimeWithMillis() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeUtil.DATETIME_FORMATTER_MILL);
        return now.format(dateTimeFormatter);
    }

    /**
     * @description 获取当前日期时间戳
     * @date 2025/3/7 8:39
     * @author chenzh
     * @param datetimeFormatter 指定日期格式，格式@see java.time.format.DateTimeFormatter
     * @return String
     */
    public static String nowDatetime(String datetimeFormatter) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(datetimeFormatter);
        return now.format(dateTimeFormatter);
    }

    /**
     * @description 将时间戳转成日期格式
     * @date 2025/3/7 8:40
     * @author chenzh
     * @param second 时间戳，单位秒
     * @return String
     */
    public static String secondToDatetime(long second) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeUtil.COMMON_DATETIME_FORMATTER);
        Instant instant = Instant.ofEpochSecond(second);

        return dateTimeFormatter.format(LocalDateTime.ofInstant(instant, ZoneId.of("+8")));
//        return dateTimeFormatter.format(LocalDateTimeUtil.of(instant, TimeZone.getTimeZone("America/New_York")));
    }

    /**
     * @description 将日期转成时间戳
     * @date 2025/3/7 8:41
     * @author chenzh
     * @param datetime 日期字符串
     * @return long
     */
    public static long datetimeToSecond(String datetime) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(TimeUtil.COMMON_DATETIME_FORMATTER));
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * @description 将日期转成时间戳
     * @date 2025/3/7 8:42
     * @author chenzh
     * @param datetime 日期字符串
     * @param formatter 日期的格式
     * @return long
     */
    public static long datetimeToSecond(String datetime, String formatter) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(formatter));
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * @description 将日期转成时间戳
     * @date 2025/3/7 8:42
     * @author chenzh
     * @param datetime 日期字符串
     * @param zoneOffset 日期所属的时区
     * @return long
     */
    public static long datetimeToSecond(String datetime, ZoneOffset zoneOffset) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(TimeUtil.COMMON_DATETIME_FORMATTER));
        return localDateTime.toEpochSecond(zoneOffset);
    }

    /**
     * @description 将日期转成时间戳
     * @date 2025/3/7 8:43
     * @author chenzh
     * @param datetime 日期字符串
     * @param formatter 日期的格式
     * @param zoneOffset 日期所属的时区
     * @return long
     */
    public static long datetimeToSecond(String datetime, String formatter, ZoneOffset zoneOffset) {
        LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(formatter));
        return localDateTime.toEpochSecond(zoneOffset);
    }
}
